<template>
  <div class="login-box">
    <div class="login">
      <h1 class="title fff">超级外卖管理系统</h1>

      <!-- 表单区域 -->
      <el-form
        :model="loginForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="account">
          <el-input
            type="text"
            v-model="loginForm.account"
            prefix-icon="iconfont icon-yonghu"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            :type="flag ? 'password' : 'text'"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-mima"
            autocomplete="off"
            @keyup.native.13="submitForm('ruleForm')"
          >
            <template #suffix>
              <i
                class="iconfont cp"
                :class="flag ? 'icon-eyes' : 'icon-eyes-'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            v-waves
            type="primary"
            @click="submitForm('ruleForm')"
            :loading="loading"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
/* 引入接口文件 */
import { login } from '@/api/user.js'
/* 引入登入验证 */
import { validateuserName, validatePass } from '@/utils/validation'
/* 计算路由 */
import { separationRouter } from '@/router/index'
/* 引入本地存储 */
import local from '@/utils/local'
/* 引入轻提示 */
import { warning } from '@/utils/hint'
export default {
  data() {
    return {
      loginForm: {
        account: '',
        password: ''
      },
      flag: true,
      loading: false,
      rules: {
        account: [{ validator: validateuserName, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }]
      }
    }
  },

  methods: {
    submitForm(formName) {
      /* 把按钮变为等待 */
      this.loading = true
      this.$refs[formName].validate(async (valid) => {
        /* 验证通过 */
        if (valid) {
          /* 拿到响应码 和数据 */
          const { code, token, role } = await login(this.loginForm)
          if (code === 0) {
            local.set('token', token)
            local.set('role', role)
            separationRouter()
            setTimeout(() => this.$router.push('/'), 1500)
          } else {
            setTimeout(() => (this.loading = false), 1500)
          }
          /* 表单验证不通过 */
        } else {
          await warning('请检查输入是否正确').then(() => {
            this.loading = false
          })
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.login-box {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: rgb(45, 58, 75);
  .login {
    width: 300px;
    text-align: center;
  }
  .title {
    letter-spacing: 2px;
    margin-bottom: 20px;
    font-size: 20px;
    font-weight: 700;
    font-family: '华文行楷', 'Courier', 'Microsoft YaHei';
  }
}
.el-button {
  width: 100%;
}
</style>
